package com.wdf.dota;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wdf.rest.service.GameForumRestClient;

public class LoginActivity  extends Activity {

	Button cancelLogin ;
	Button registerLogin;
	Button submitlLogin ;
	EditText usernameEditText;
	EditText passwordEditText;
	
	LoginRestService loginRestService;
	
	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		cancelLogin = (Button)this.findViewById(R.id.cancel_login);
		cancelLogin.setOnClickListener(cancelLoginButtonListener);
		registerLogin = (Button)this.findViewById(R.id.register);
		registerLogin.setOnClickListener(registerLoginButtonListener);
		usernameEditText = (EditText)this.findViewById(R.id.my_username);
		passwordEditText = (EditText)this.findViewById(R.id.my_password);
		
		submitlLogin = (Button)this.findViewById(R.id.submit_login);
		submitlLogin.setOnClickListener(submitlLoginButtonListener);
		loginRestService = new LoginRestService(LoginActivity.this);
		
	}
	
	
	OnClickListener cancelLoginButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			LoginActivity.this.finish();
		}
	};
	
	OnClickListener registerLoginButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			 Intent intent = new Intent(LoginActivity.this,
        			 RegisterActivity.class);
        	  startActivityForResult(intent, 4);
		}
	};
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	            super.onActivityResult(requestCode, resultCode, data);
	            SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
	    				Context.MODE_PRIVATE);
	    	String	username = sharedPreferences.getString("username", "");
	    	String	accountId = sharedPreferences.getString("accountid", "0");

	    		if("0".equals(accountId)||"".equals(username)){
	    			
	    		}else{
	    			
	    		this.finish();

	    		}           
	}
	
	OnClickListener submitlLoginButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			 String username = usernameEditText.getText().toString();
			 String password = passwordEditText.getText().toString();
			 if(username ==null || "".equals(username.trim())){
				 Toast.makeText(LoginActivity.this, "请输入用户名或昵称!", 2).show();
				  return ;
			 }
			 if(password ==null || "".equals(password.trim())){
				 Toast.makeText(LoginActivity.this, "请输入密码!", 2).show();
				  return ;
			 }
			 loginRestService.login(username, password);
		}
	};
	
	class LoginRestService {
		

        private Context  context;
        
        public LoginRestService(Context context){

        	this.context = context;
        }

      
		 public  void  login(final String username, final String password){
	 			final RequestParams params = new RequestParams();
				 params.put("username", username);
				 params.put("password", password);
				 try {
					 GameForumRestClient.get( "/users/login.json", params, new JsonHttpResponseHandler() {

						 @Override
				            public void onSuccess(JSONObject object) {
				            	 if(object!=null ){
				            		   try {
										String mess = object.getString("mess");
										if(mess !=null && mess.length()>0){
											Toast.makeText(context, mess, 2).show();
										}else{
											String accountid = object.getString("id");
											String username = object.getString("username");
											 SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
														Context.MODE_PRIVATE);
							            		Editor editor = sharedPreferences.edit();
							            		editor.putString("username", username);
							            		editor.putString("accountid", accountid);
							            		editor.putString("accountId", accountid);
							            		editor.commit();
							            		LoginActivity.this.finish();
										}
									} catch (JSONException e) {
										Toast.makeText(context, "登录失败", 2).show();
									}
				            		   
				            	 }
				            	
				            }
				           
				            
				        });
 
				} catch (Exception e) {
					
					e.printStackTrace();
				}
				 
			 }	
	}
}

